//
//  LGController.m
//  ALS-02
//
//  Created by guomianxiang on 16/11/8.
//  Copyright © 2016年 guomianxiang. All rights reserved.
//

#import "LGController.h"
#import "ALSConstNSString.h"
#import <objc/runtime.h>

#define  CHYControllerInfoPreFix  @"terminal_broadcast_info:"
#define  CHYContrillerPin_code @"pin_code:"
#define  CHYContrillerIP @"ip:"
#define  CHYContrillerMac @"mac:"

@implementation LGController


+ (instancetype)controllerWithData:(NSData *)data {
    LGController *controller = [[LGController alloc]initWithData:data];
    return controller;
}

- (instancetype)initWithData:(NSData *)data {

    if (self = [super init]) {
        self.search = YES;
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        if (dataStr.length) {
            
            if ([dataStr hasPrefix:CHYControllerInfoPreFix]) { //在这里解析数据
                
                //将字符串转化为数组
                
                NSArray *files = [dataStr componentsSeparatedByString:@","];
                
                for (NSString *file in files) {
                    
                    if ([file hasPrefix:CHYContrillerPin_code]) {
                        
                        NSString *pin_code = [file substringFromIndex:CHYContrillerPin_code.length];
                        self.sn = pin_code;
                    }
                    
                    if ([file hasPrefix:CHYContrillerIP]) {
                        
                        NSString *ip = [file substringFromIndex:CHYContrillerIP.length];
                        
                        self.ip = ip;
                    }
                    
                    if ([file hasPrefix:CHYContrillerMac]) {
                        
                        NSString *mac = [file substringFromIndex:CHYContrillerMac.length];
                        
                        self.mac = mac;
                    }
                    
                }
                
            } else{
                
                return nil;
            }
            
        }
    }
    
    return self;

}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_name forKey:@"name"];
    [aCoder encodeObject:_sn forKey:@"sn"];
    [aCoder encodeObject:_ip forKey:@"ip"];
    [aCoder encodeObject:_port forKey:@"port"];
    [aCoder encodeObject:_doors forKey:@"door"];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        _name = [aDecoder decodeObjectForKey:@"name"];
        _sn = [aDecoder decodeObjectForKey:@"sn"];
        _ip = [aDecoder decodeObjectForKey:@"ip"];
        _port = [aDecoder decodeObjectForKey:@"port"];
        _doors = [aDecoder decodeObjectForKey:@"door"];
    }
    return self;
}

- (id)copyWithZone:(NSZone *)zone {
    LGController *copyItem = [[[self class] allocWithZone:zone]init];
    copyItem.name = self.name;
    copyItem.ip = self.ip;
    copyItem.sn = self.sn;
    copyItem.port = self.port;
    copyItem.doors = [self.doors copy];
    return copyItem;
}



- (NSString *)description {
//    return [NSString stringWithFormat:@"name:%@\n sn:%@\n ip:%@\n port:%@\n door:%@",self.name,self.sn,self.ip,self.port,self.doors];
    return [NSString stringWithFormat:@"ip : %@\n sn: %@\n mac:%@",self.ip,self.sn,self.mac];
}

@end

